# -*- coding:UTF-8 -*-

from fabric.api import *
from fabric.contrib.files import exists

import os

BASE_PATH = '/opt/zhangsan/app'
APP_PATH = BASE_PATH + '/data-center/homepage/backend'

HOSTS = ['zhangsan@192.168.1.100']
env.password='123456'

def getVersion():
    #
    # 动态从版本配置文件中读取当前最新版本号
    #
    file_path = VERSION_CONFIG_PATH
    props = parse(file_path)   #读取文件
    print('version:'+props.get('VERSION'))
    return props.get('VERSION')            #根据key读取value

def parse(file_name):
    return Properties(file_name)

def scripts():
    APP_NAME = 'example-1.0.1.jar'
    JAR_PATH = 'build/libs/' + APP_NAME
    local('pwd')
    if not exists(APP_PATH):
        run('mkdir -p ' + APP_PATH)
    if exists(APP_PATH + '/application.pid'):
        with settings(warn_only=True):
            run('kill -9 $(cat ' + APP_PATH + '/application.pid)')
    with settings(warn_only=True):
        run('rm -Rf ' + APP_PATH + '/*.*')
    put(JAR_PATH, APP_PATH)
    #put(PROPERTIES_FILE, APP_PATH)
    with cd(APP_PATH):
        run('$(nohup java -jar -Xmx2g ' + APP_NAME + ' --spring.profiles.active=test >& /dev/null < /dev/null &) && sleep 1')

def deploy(hosts):
    execute(scripts, hosts=hosts)

if __name__=="__main__":
    deploy(HOSTS)

